package com.gaohe.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaohe.mapper.OrderMapper;
import com.gaohe.pojo.AddressBook;
import com.gaohe.pojo.Orders;
import com.gaohe.pojo.ShoppingCart;
import com.gaohe.pojo.User;
import com.gaohe.service.AddressBookService;
import com.gaohe.service.AlipayService;
import com.gaohe.service.OrderService;
import com.gaohe.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class OrderServiceImp extends ServiceImpl<OrderMapper, Orders> implements OrderService {
    @Autowired
    HttpServletRequest request;
    @Autowired
    AddressBookService addressBookService;
    @Autowired
    ShoppingCartService shoppingCartService;
    @Autowired
    private AlipayService alipayService;

    @Override
    public Page<Orders> pageOder(Integer page, Integer pageSize) {
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        User user = (User) request.getSession().getAttribute("user");
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Orders::getUserId, user.getId());
        this.page(ordersPage, lqw);
        return ordersPage;

    }

    @Override
    @Transactional
    public String subOrders(Orders orders) {
        User user = (User) request.getSession().getAttribute("user");
        orders.setUserId(user.getId());
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        //查询地址表的信息
        AddressBook addressBook = addressBookService.getById(orders.getAddressBookId());
        orders.setPhone(addressBook.getPhone());
        orders.setUserName(orders.getUserName());
        orders.setConsignee(addressBook.getConsignee());
        orders.setUserName(addressBook.getConsignee());
        orders.setAddress(addressBook.getDetail());
        //查询购物车表的信息
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, user.getId());
        List<ShoppingCart> shoppingCarts = shoppingCartService.list(lqw);
        //生成唯一订单号
        long num = IdWorker.getId();
        String s = String.valueOf(num);
        orders.setNumber(s);
        //金额
        BigDecimal money = BigDecimal.ZERO;
        BigDecimal reduce = shoppingCarts.stream().map(shoppingCart -> {
            BigDecimal amount = shoppingCart.getAmount();
            return money.add(amount);
        }).reduce(BigDecimal.ZERO, BigDecimal::add);
        orders.setAmount(reduce);
        this.save(orders);
        //支付
        String s1 = alipayService.tradeCreate(orders);
        //删除购物车的信息
        shoppingCartService.remove(lqw);
        return s1;
    }

    @Override
    public Page<Orders> ORDERS_PAGE(Integer page, Integer pageSize, Long number, String beginTime, String endTime) {
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        log.info("{},{}",beginTime,endTime);

        //查询orders表
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.like(number != null, Orders::getNumber, number);
        lqw.eq(beginTime != null, Orders::getOrderTime, beginTime);
        lqw.eq(endTime != null, Orders::getCheckoutTime, endTime);
        this.page(ordersPage, lqw);
        return ordersPage;

    }

}
